/*
 *  bone.cc
 *  wonderland-project
 *
 *  Created by Ben Nolan on 20/10/08.
 *  Copyright 2008 Nolan Consulting Limited. All rights reserved.
 *
 */

#include "bone.h"

#include <OgreVector3.h>
#include <OgreQuaternion.h>
#include <OgreBone.h>

#include "vector.h"
#include "quaternion.h"
#include "v8defines.h"
#include "scripting_context.h"

namespace JSBone{
  
  v8::Handle<v8::ObjectTemplate> instance_template;
  v8::Handle<v8::FunctionTemplate> function_template;
  v8::Persistent<v8::Context> context_;
  
  Wonderland::Region *region_;

  V8_GETTER(Bone, name){
    return v8::String::New(V8INTERNAL(Ogre::Bone*)->getName().c_str());
  }

  /*
   * Releases any manual positioning
   */
  V8CALLBACK(Bone,release){
    V8INTERNAL(Ogre::Bone*)->setManuallyControlled(false);
    
    return v8::Undefined();
  }
  
  
  V8CALLBACK(Bone,getRotation){ 
    return JSQuaternion::New(V8INTERNAL(Ogre::Bone*)->getOrientation());
  }

  V8CALLBACK(Bone,setRotation){ 
    if (!V8ARGUMENTTYPE("Quaternion", 0))
      return ThrowException(v8::String::New("Invalid argument"));

    V8INTERNAL(Ogre::Bone*)->setManuallyControlled(true);
    V8INTERNAL(Ogre::Bone*)->setOrientation(*V8ARGUMENT(Ogre::Quaternion*, 0));
    
    return v8::Undefined();
  }

  V8CALLBACK(Bone,getPosition){ 
    return JSVector::New(V8INTERNAL(Ogre::Bone*)->getPosition());
  }

  V8CALLBACK(Bone,setPosition){ 
    if (!V8ARGUMENTTYPE("Vector", 0))
      return ThrowException(v8::String::New("Invalid argument"));

    V8INTERNAL(Ogre::Bone*)->setManuallyControlled(true);
    V8INTERNAL(Ogre::Bone*)->setPosition(*V8ARGUMENT(Ogre::Vector3*, 0));
    
    return v8::Undefined();
  }

  V8CALLBACK(Bone,getScale){ 
    return JSVector::New(V8INTERNAL(Ogre::Bone*)->getScale());
  }

  V8CALLBACK(Bone,setScale){ 
    if (!V8ARGUMENTTYPE("Vector", 0))
      return ThrowException(v8::String::New("Invalid argument"));

    V8INTERNAL(Ogre::Bone*)->setManuallyControlled(true);
    V8INTERNAL(Ogre::Bone*)->setScale(*V8ARGUMENT(Ogre::Vector3*, 0));
    
    return v8::Undefined();
  }

  /* Construct a new node - doesnt actually allocate the node, use world.createBone for that */
  V8CALLBACK(Bone,create){
    v8::Persistent<v8::Object> self = v8::Persistent<v8::Object>::New(args.Holder());
    return self;
  }

  /* Sets internal fields with our idiom of storing the class name in the second field */
  void setInternalFields(v8::Persistent<v8::Object> p, Ogre::Bone *n){
    p->SetInternalField(0, v8::External::New(n));
    p->SetInternalField(1, v8::String::New("Bone"));
  }

  /* Only called from c++ */
  v8::Handle<v8::Value> New(Ogre::Bone *bone){
    context_->Enter();

    v8::Persistent<v8::Object> instance = v8::Persistent<v8::Object>::New(function_template->GetFunction()->NewInstance());
    setInternalFields(instance, bone);
    return instance;
  }

  void Initialize(Wonderland::ScriptingContext *sc, Wonderland::Region *region){
    context_ = sc->getContext();
    region_ = region;

    // Function Template
    function_template = v8::FunctionTemplate::New(Bone_create);
    function_template->SetClassName(v8::String::New("Bone"));

    // Instance template
    instance_template = function_template->InstanceTemplate();
    instance_template->SetInternalFieldCount(2);

    #define V8REGISTER(function) instance_template->Set(#function, v8::FunctionTemplate::New(Bone_ ## function));
    
    instance_template->SetAccessor(v8::String::New("name"), Bone_getname);

    V8REGISTER(release);
    V8REGISTER(getRotation);
    V8REGISTER(setRotation);
    V8REGISTER(getPosition);
    V8REGISTER(setPosition);
    V8REGISTER(getScale);
    V8REGISTER(setScale);
  }

}

